package com.youxue.teachin.set.child.xuekexueduan;

import java.util.List;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import com.library.uiframework.fragment.BaseFragment;
import com.library.uiframework.paging.ColorDividerItemDecoration;
import com.library.uiframework.paging.SpaceGridItemDecoration;
import com.library.uiframework.utils.DisplayUtils;
import com.library.xtype.NuwaRecyclerViewAdapter;
import com.youxue.teachin.R;
import com.youxue.teachin.common.vo.UserVO;
import com.youxue.teachin.set.child.my.IChageUserView;
import com.youxue.teachin.set.child.my.UserChangePresener;
import com.youxue.teachin.set.child.xuekexueduan.binder.ItemBinder;
import com.youxue.teachin.set.child.xuekexueduan.vo.ItemVO;
import com.youxue.teachin.set.repository.po.UserPO;
import com.youxue.teachin.user.userSystem.UserSystem;

/**
 * 学科学段设置
 *
 * @author: zhiwei
 * @date: 2017-10-07 16:29
 * @version: 9.1.0
 */
public class XueKeXueDuanFragment extends BaseFragment implements IXuekeXueduanView, OnClickListener, IChageUserView{

    private View mRootView;
    private RecyclerView mStudyStages;
    private RecyclerView mStudySubject;
    //学段
    private NuwaRecyclerViewAdapter mStagesAdapter;
    //学科
    private NuwaRecyclerViewAdapter mSubjectAdapter;

    XuekeXueduanPresenter mPresenter = new XuekeXueduanPresenter(this);

    UserChangePresener mUserChangePresener = null;

    private ItemVO mStudyStagesItem;
    private ItemVO mStudySUbjectItem;

    public void comment() {
        if (mStudySUbjectItem == null && mStudyStagesItem == null) {
            return;
        }
        if (mUserChangePresener == null) {
            mUserChangePresener = new UserChangePresener(this);
            registerPageLifecycle(mUserChangePresener);
        }
        UserPO userPO = new UserPO();
        if (mStudyStagesItem != null) {
            userPO.studyStagesId = mStudyStagesItem.id;
        }
        if (mStudySUbjectItem != null) {
            userPO.studySubjectId = mStudySUbjectItem.id;
        }
        mUserChangePresener.updateUser(userPO);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.set_xuekexueduan_layout, container, false);
        initView(view);
        registerPageLifecycle(mPresenter);
        return view;
    }

    private void initView(View rootView) {
        mRootView = rootView;
        mStudyStages = (RecyclerView)rootView.findViewById(R.id.studyStages);
        mStudySubject = (RecyclerView)rootView.findViewById(R.id.studySubject);
        mStudyStages.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        mStudySubject.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        int gap = DisplayUtils.dp2px(15);
        mStudyStages.addItemDecoration(new ColorDividerItemDecoration(Color.TRANSPARENT, gap, LinearLayoutManager.HORIZONTAL));
        mStudySubject.addItemDecoration(new SpaceGridItemDecoration(gap, 4));

        mStagesAdapter = new NuwaRecyclerViewAdapter();
        mSubjectAdapter = new NuwaRecyclerViewAdapter();
        mStagesAdapter.register(ItemBinder.class);
        mSubjectAdapter.register(ItemBinder.class);

        mSubjectAdapter.setOnClickListener(this);
        mStagesAdapter.setOnClickListener(this);
        mStudyStages.setAdapter(mStagesAdapter);
        mStudySubject.setAdapter(mSubjectAdapter);
        mPresenter.loadData();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    public void bindData(List stages, List subject) {
        mStagesAdapter.flushData(stages);
        mSubjectAdapter.flushData(subject);
    }

    View mStudyStagesView;
    View mStudySubjetView;

    @Override
    public void onClick(View v) {
        ItemVO itemVO = (ItemVO)v.getTag();
        if (itemVO.type == ItemVO.TYPE_STAGES) {
            if (mStudyStagesView != null) {
                mStudyStagesView.setSelected(false);
            }
            mStudyStagesView = v;
            v.setSelected(true);
            mStudyStagesItem = itemVO;
        } else {
            mStudySUbjectItem = itemVO;
            if (mStudySubjetView != null) {
                mStudySubjetView.setSelected(false);
            }
            mStudySubjetView = v;
            v.setSelected(true);
        }
    }

    @Override
    public void updateSucces() {
        UserVO userVO = UserSystem.getInstance().getUserVO();
        if (mStudySUbjectItem != null) {
            userVO.studySubjectId = mStudySUbjectItem.name;
        }
        if (mStudyStagesItem != null) {
            userVO.studyStagesId = mStudyStagesItem.name;
        }
        finish();
    }

    @Override
    public void updateFail() {

    }
}
